package com.chuanjiabao.wisdom.util.dialog;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;

import com.bumptech.glide.Glide;
import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.databinding.DialogSecretMakeBinding;
import com.chuanjiabao.wisdom.listener.OnAnimatorListener;
import com.chuanjiabao.wisdom.listener.OnBHClickListener;
import com.flyco.animation.SlideEnter.SlideBottomEnter;
import com.flyco.animation.SlideExit.SlideBottomExit;
import com.flyco.dialog.widget.base.BaseDialog;
import com.fzq.cameralib.util.ScreenUtils;

import java.util.concurrent.TimeUnit;

import io.reactivex.Completable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class SecretMakeDialog extends BaseDialog {

    private Context context;
    private String hint;
    private Disposable disposable;
    private DialogSecretMakeBinding binding;
    private OnBHClickListener onBHClickListener;

    public SecretMakeDialog(Context context, String hint) {
        super(context);
        this.context = context;
        this.hint = hint;
    }

    @Override
    public View onCreateView() {
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.dialog_secret_make, null, false);
        binding.setDialog(this);
        return binding.getRoot();
    }

    @Override
    public void setUiBeforShow() {
        showAnim(new SlideBottomEnter());
        dismissAnim(new SlideBottomExit());
        binding.tvModuleHint.setText(hint);

        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.height = ScreenUtils.getScreenHeight(context); //设置宽度
        lp.width = ScreenUtils.getScreenWidth(context);   //设置宽度
        getWindow().setAttributes(lp);

        Animation leftAnimation = AnimationUtils.loadAnimation(context, R.anim.secret_left_in);
        leftAnimation.setInterpolator(new LinearInterpolator());
        binding.llModuleDelete.startAnimation(leftAnimation);

        ObjectAnimator translationY = ObjectAnimator.ofFloat(binding.flModuleTop, "translationY", -500f, 300f);
        translationY.setDuration(500);
        translationY.start();
        translationY.addListener(new OnAnimatorListener() {
            @SuppressLint("CheckResult")
            @Override
            public void onAnimationEnd(Animator animation) {
                Glide.with(context)
                        .asGif()
                        .load(R.mipmap.icon_anim_unlock)
                        .into(binding.ivModuleImg);

                //noinspection ResultOfMethodCallIgnored
                disposable = Completable.timer(2000, TimeUnit.MILLISECONDS)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(() -> {
                            binding.ivModuleImg.setImageResource(R.mipmap.icon_unlock);
                            startAnim();
                        });
            }
        });

        binding.ivModuleBg.setVisibility(View.GONE);
        binding.llModuleBottom.setVisibility(View.GONE);
    }

    private void startAnim() {
        ObjectAnimator translationY = ObjectAnimator.ofFloat(binding.flModuleTop, "translationY", 300, 0);
        translationY.setDuration(500);
        translationY.start();
        translationY.addListener(new OnAnimatorListener(){
            @Override
            public void onAnimationEnd(Animator animation) {
                binding.ivModuleBg.setVisibility(View.VISIBLE);
                binding.llModuleBottom.setVisibility(View.VISIBLE);

                Animation rotateAnimation = AnimationUtils.loadAnimation(context, R.anim.rotate_anim);
                rotateAnimation.setInterpolator(new LinearInterpolator());
                binding.ivModuleBg.startAnimation(rotateAnimation);

                Animation bottomAnimation = AnimationUtils.loadAnimation(context, R.anim.secret_bottom_in);
                bottomAnimation.setInterpolator(new LinearInterpolator());
                binding.llModuleBottom.startAnimation(bottomAnimation);
            }
        });
    }

    public void setOnBHClickListener(OnBHClickListener onBHClickListener) {
        this.onBHClickListener = onBHClickListener;
    }

    public void noPoint(View view) {
        if (disposable != null) disposable.dispose();
        dismiss();
    }

    public void okPoint(View view) {
        if (onBHClickListener != null) {
            onBHClickListener.onConfirmClick();
        }
    }

}


